// pages/shops/shoplist.js
var http = require('../../utils/request');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTab: [
      '全部', '长保', '短保'
    ],
    currentTab: 0,
    shopList: [],
    typeList: {
      1: "常温",
      2: "冷藏",
      3:"FF",
      4:"冷冻"
    },
    statusList: {
      1: "长保",
      2: "短保"
    },
    page: 1,
    size: 10,
    isLoadMore: false,
    loadStatus: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({page: 1, isLoadMore: true});
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (! that.data.isLoadMore) { // 此处判断，上锁，防止重复请求
      that.setData({
        page: that.data.page + 1,
        isLoadMore: true
      });
      that.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  currentTab: function (e) {
    let that = this;
    if (that.data.currentTab == e.currentTarget.dataset.idx) {
      return;
    }
    that.setData({currentTab: e.currentTarget.dataset.idx})
    that.getData()
  },
  getData: function () {
    var that = this;
    // 1全部 2长保 3短保
    // 预警列表 含分页
    http.postData('/xiandu/app/v1/get_warn_list', {
      type: that.data.currentTab + 1,
      page: that.data.page,
      size: that.data.size
    }, (rep) => {
      wx.stopPullDownRefresh();
      if (rep.code == 200200) {
        let datas = rep.data.list;
        that.setData({shopList: that.data.page == 1 ? datas: that.data.shopList.concat(datas)});
        if (datas.length < that.data.size) { // 小于size数，停止请求
          that.setData({isLoadMore: true, loadStatus: '没有更多了'});
        } else {
          that.setData({isLoadMore: false});
        }
      }
    })
  },
  delOPt: function (id) {
    let that = this;
    http.postData('/xiandu/app/v1/checkout_app_batchwarn', {
      warn_message_id_str:id
    }, (rep) => {
          if (rep.code == 200200) {
            wx.showToast({title: '核销成功', icon: 'none', duration: 2000})
            that.setData({page: 1, isLoadMore: true});
            that.getData();
          }
    }) 
  },
  showModal: function (e) {
    let warnid= e.target.dataset.warnid;
        wx.showModal({
          title: '提示',
          content: '确定要核销该商品吗？',
          showCancel: true, // 是否显示取消按钮
          success: res => {
            if (res.cancel) { // 点击取消,默认隐藏弹框
            } else { // 点击确定
              this.delOPt(warnid);
            }
          },
          fail: function (res) {}, // 接口调用失败的回调函数
        })
  }
})
